﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GoShopping.BLL;
using System.Web.Security;
using System.Data;
using GoShopping.Model;
using System.Net.Configuration;

namespace GoShopping.Web.SuperManager
{
    public partial class ManagerPageL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["index"] != null)
                {
                    JumpToView();
                    return;
                }
                bindSysUser();
            }
        }

        private void JumpToView()
        {
            MultiView1.ActiveViewIndex = Convert.ToInt16(Request.QueryString["index"]);
            switch (Convert.ToInt16(Request.QueryString["index"]))
            {
                case 0:
                    {
                        bindSysUser();
                    } break;
                case 1:
                    {
                        BindProUser();
                    } break;
                case 2:
                    {
                        bindSysUser();
                    } break;
                case 4:
                    {
                        bindEmailTemp();
                    } break;
            }
        }

        #region 绑定事件

        /// <summary>
        /// 绑定商品管理员
        /// </summary>
        void BindProUser()
        {
            ProductManagerBean userBean = new ProductManagerBean();            
            IList<GoShopping.Model.ProductManager> userList = userBean.GetProductManagerList();         
            gvUserList.DataSource = userList;
            gvUserList.DataBind();
        }
        
        /// <summary>
        /// 绑定系统管理员
        /// </summary>
        void bindSysUser()
        {
            UserManagerBean userMBean = new UserManagerBean();
            IList<UserManager> userList = userMBean.GetUserManagerList();
            gvSysUserList.DataSource = userList;
            gvSysUserList.DataBind();

            gvDonwMList.DataSource = userList;
            gvDonwMList.DataBind();
        }

        /// <summary>
        /// 绑定邮件模板列表
        /// </summary>
        void bindEmailTemp()
        {            
            EmailTempleteBean emailTempl = new EmailTempleteBean();
            IList<EmailTemplete> emailList = emailTempl.GetAllEmailTemplete();
            gvEmailTemp.DataSource = emailList;
            gvEmailTemp.DataBind();
        }
        
        #endregion 
           
        #region 按钮事件

        /// <summary>
        /// 添加产品管理员的按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            Response.Redirect("SupMenOperatorC.aspx?Type=AddProductM");
        }
      
        /// <summary>
        /// 添加系统管理员的按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddSysM_Click(object sender, EventArgs e)
        {
            Response.Redirect("SupMenOperatorC.aspx?Type=AddSysM");
        }

        /// <summary>
        /// 添加下载管理员的按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDownM_Click(object sender, EventArgs e)
        {
            Response.Redirect("SupMenOperatorC.aspx?Type=AddDownM");
        }
          
        /// <summary>
        /// 系统管理员导航按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LBSysM_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
            Cell1.Attributes["class"] = "SelectedTopBorder";
            Cell2.Attributes["class"] = "TopBorder";
            //Cell3.Attributes["class"] = "TopBorder";
            //Cell4.Attributes["class"] = "TopBorder";
            Cell5.Attributes["class"] = "TopBorder";
            bindSysUser();
        }

        /// <summary>
        /// 商品管理员导航按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LBProM_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
            Cell1.Attributes["class"] = "TopBorder";
            Cell2.Attributes["class"] = "SelectedTopBorder";
            //Cell3.Attributes["class"] = "TopBorder";
            //Cell4.Attributes["class"] = "TopBorder";
            Cell5.Attributes["class"] = "TopBorder";
            BindProUser();
        }
        
        /// <summary>
        /// 下载管理员按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LBDownM_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 2;
            Cell1.Attributes["class"] = "TopBorder";
            Cell2.Attributes["class"] = "TopBorder";
            //Cell3.Attributes["class"] = "SelectedTopBorder";
            //Cell4.Attributes["class"] = "TopBorder";
            Cell5.Attributes["class"] = "TopBorder";
            //bindSysUser();
        }

        ///// <summary>
        ///// STMP设置按钮事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void LBEmailSet_Click(object sender, EventArgs e)
        //{
        //    MultiView1.ActiveViewIndex = 3;
        //    Cell1.Attributes["class"] = "TopBorder";
        //    Cell2.Attributes["class"] = "TopBorder";
        //    Cell3.Attributes["class"] = "TopBorder";
        //    Cell4.Attributes["class"] = "SelectedTopBorder";
        //    Cell5.Attributes["class"] = "TopBorder";
        //    bindEmailSet();
        //}

        /// <summary>
        /// 邮件模板设置按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LBEmTempl_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 3;
            Cell1.Attributes["class"] = "TopBorder";
            Cell2.Attributes["class"] = "TopBorder";
            //Cell3.Attributes["class"] = "TopBorder";
            //Cell4.Attributes["class"] = "TopBorder";
            Cell5.Attributes["class"] = "SelectedTopBorder";
            bindEmailTemp();
        }

        /// <summary>
        /// 设置邮件模板的按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddEmailTempl_Click(object sender, EventArgs e)
        {
            Response.Redirect("EmailTemplet.aspx");
        }
        #endregion

        #region RoeDataBound事件

        /// <summary>
        /// 系统管理员GridView的RowDataBound事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSysUserList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridViewRow BoundRow = e.Row;
                DataRowView drv = BoundRow.DataItem as DataRowView;

                ImageButton imgSysDel = BoundRow.FindControl("imgSysDel") as ImageButton;
                if (imgSysDel != null)
                {
                    imgSysDel.Attributes.Add("onclick", "return confirm('您确认要删除选定的信息吗？')");
                    imgSysDel.CommandName = "DelSysMUser";
                }

                ImageButton imgSysEdit = BoundRow.FindControl("imgSysEdit") as ImageButton;
                if (imgSysEdit != null)
                {
                    imgSysEdit.Attributes.Add("onclick", string.Format("window.location='SupMenOperatorC.aspx?index=0&Role=UserRole&ID=" + ((UserManager)(BoundRow.DataItem)).ID + "'"));
                }
            }
        }

        /// <summary>
        /// 商品管理员GridView的RowDataBound事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvUserList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridViewRow BoundRow = e.Row;
                DataRowView drv = BoundRow.DataItem as DataRowView;

                ImageButton imgDel = BoundRow.FindControl("imgDel") as ImageButton;
                if (imgDel != null)
                {
                    imgDel.Attributes.Add("onclick", "return confirm('您确认要删除选定的信息吗？')");
                    imgDel.CommandName = "DelMangerUser";
                }

                ImageButton imgEdit = BoundRow.FindControl("imgEdit") as ImageButton;
                if (imgEdit != null)
                {
                    imgEdit.Attributes.Add("onclick", string.Format("window.location='SupMenOperatorC.aspx?index=1&Role=ProRole&ID=" + ((GoShopping.Model.ProductManager)(BoundRow.DataItem)).ID + "'"));
                }
            }
        }

        /// <summary>
        /// 邮件模板设置GridView的RowDataBound事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvEmailTemp_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridViewRow BoundRow = e.Row;
                DataRowView drv = BoundRow.DataItem as DataRowView;

                ImageButton imgEMPDel = BoundRow.FindControl("imgEMPDel") as ImageButton;
                if (imgEMPDel != null)
                {
                    imgEMPDel.Attributes.Add("onclick", "return confirm('您确认要删除选定的信息吗？')");
                    imgEMPDel.CommandName = "DelEMP";
                }

                ImageButton imgEMPEdit = BoundRow.FindControl("imgEMPEdit") as ImageButton;
                if (imgEMPEdit != null)
                {
                    imgEMPEdit.Attributes.Add("onclick", string.Format("window.location='EmailTemplet.aspx?Type=Update&Name=" + ((EmailTemplete)(BoundRow.DataItem)).Name + "'"));
                }
            }
        }
        
        #endregion

        #region RowCommand事件

        /// <summary>
        /// 系统管理员GridView的RowCommand事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSysUserList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DelSysMUser")
            {

                MemberShipService memberShipserver = new MemberShipService();
                string UserName = e.CommandArgument.ToString();
                bool isDel = memberShipserver.DeleteMember(UserName);
                if (isDel)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "click", "alert('用户" + UserName + "已经被删除!');", true);
                    bindSysUser();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "click", "alert('用户" + UserName + "删除失败!');", true);
                }
            }
        }
        
        /// <summary>
        ///  商品管理员GridView的RowCommand事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvUserList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DelMangerUser")
            {

                MemberShipService memberShipserver = new MemberShipService();
                string UserName = e.CommandArgument.ToString();
                bool isDel = memberShipserver.DeleteMember(UserName);
                if (isDel)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "click", "alert('用户" + UserName + "已经被删除!');", true);
                    BindProUser();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "click", "<script>alert('用户" + UserName + "删除失败!');</script>", true);
                }
            }
        }

        /// <summary>
        /// 邮件模板GridView的RowCommand事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvEmailTemp_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DelEMP")
            {
                BLL.EmailTempleteBean etb = new EmailTempleteBean();
                string etId = e.CommandArgument.ToString();
                bindEmailTemp();
                //bool isDel = memberShipserver.DeleteMember(UserName);
                //if (isDel)
                //{
                //    ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "click", "alert('用户" + UserName + "已经被删除!');", true);
                //    BindProUser();
                //}
                //else
                //{
                //    ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "click", "<script>alert('用户" + UserName + "删除失败!');</script>", true);
                //}
            }
        }
        #endregion
}       
   
}
